//
//  ClassListViewController.m
//  huashiedu
//
//  Created by oceans on 2018/8/27.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "ClassListViewController.h"
#import "ClassListTableViewCell.h"
#import "ClassListTableHeaderView.h"
#import "ClassListTypeCollectionViewCell.h"
#import "ClassListSubtypeCollectionViewCell.h"
#import "SearchListViewController.h"
#import "HttpRequest.h"
#import "ClassModel.h"
#import <UIImageView+WebCache.h>
#import "JFCityViewController.h"
#import "SendRequestViewController.h"
#import "NormalWebViewController.h"
#import <MJRefresh.h>
#import "ClassListTableFooterView.h"
#import "LocationModel.h"
#import "ClassTypeObject.h"
#import "LoginViewController.h"

@interface ClassListViewController () <UITableViewDelegate, UITableViewDataSource, ClassListTableHeaderViewDelegate, UIGestureRecognizerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, ClassListTableFooterViewDelegate>

@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSMutableArray <ClassModel *> *dataArr;
@property (strong, nonatomic) UITableView *selectTableView;
@property (strong, nonatomic) UIView *coverView;
@property (strong, nonatomic) NSArray <NSArray *> *selectArray;
@property (strong, nonatomic) NSArray <NSArray *> *selectValueArray;
/**显示第几个 tableview*/
@property (assign, nonatomic) NSInteger selectIndex;
/**排序等，选择了第几个*/
//@property (assign, nonatomic) NSInteger areaSelect;
@property (assign, nonatomic) NSInteger sortSelect;
@property (assign, nonatomic) NSInteger timeSelect;
@property (weak, nonatomic) UIButton *selectButton;
@property (strong, nonatomic) UICollectionView *typeCollection;
@property (strong, nonatomic) UICollectionView *subtypeCollection;
@property (strong, nonatomic) NSArray <ClassTypeArrayModel *> *typeArr;

@property (assign, nonatomic) NSInteger subtypeSelect;
@property (strong, nonatomic) CALayer *lineLayer;
@property (strong, nonatomic) MetaModel *model;

@property (strong, nonatomic) NSDictionary *paras;
@property (strong, nonatomic) NSArray <ClassModel*> *recommendsArr;
@property (assign, nonatomic) BOOL selectingArea;
@property (copy, nonatomic) NSString *areaCode;

@property (strong, nonatomic) UIView *tableHeader;
@end

@implementation ClassListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _subtypeSelect = -1;
    [self setupViews];
    if ([LocationModel shared].areaNumber.length > 0) {
        self.areaCode = [LocationModel shared].areaNumber;
    }else {
        self.areaCode = nil;
    }
    if ([LocationModel shared].areaName.length > 0) {
        [self.selectButton setTitle:[LocationModel shared].areaName forState:UIControlStateNormal];
    }else {
        [self.selectButton setTitle:@"全部区域" forState:UIControlStateNormal];
    }
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_selectingArea) {
        _selectingArea = NO;
        if ([LocationModel shared].areaNumber.length > 0) {
            self.areaCode = [LocationModel shared].areaNumber;
        }else {
            self.areaCode = nil;
        }
        if ([LocationModel shared].areaName.length > 0) {
            [self.selectButton setTitle:[LocationModel shared].areaName forState:UIControlStateNormal];
        }else {
            [self.selectButton setTitle:@"全部区域" forState:UIControlStateNormal];
        }
        [self loadData];
    }
}

- (void)setupViews {
    
    self.navigationItem.title = @"课程类别";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:0];
    [button setImage:[UIImage imageNamed:@"searchIcon"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(searchButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 44, 44);
    UIView *view = [[UIView alloc] initWithFrame:button.bounds];
    [view addSubview:button];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.typeCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0) collectionViewLayout:layout];
    _typeCollection.backgroundColor = [UIColor whiteColor];
    _typeCollection.delegate = self;
    _typeCollection.dataSource = self;
    [_typeCollection registerClass:[ClassListTypeCollectionViewCell class] forCellWithReuseIdentifier:@"ClassListTypeCollectionViewCell"];
    
    UICollectionViewFlowLayout *layout2 = [[UICollectionViewFlowLayout alloc] init];
    layout2.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.subtypeCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 50) collectionViewLayout:layout2];
    _subtypeCollection.backgroundColor = [UIColor whiteColor];
    _subtypeCollection.delegate = self;
    _subtypeCollection.dataSource = self;
    [_subtypeCollection registerClass:[ClassListSubtypeCollectionViewCell class] forCellWithReuseIdentifier:@"ClassListSubtypeCollectionViewCell"];
    _subtypeCollection.tag = 1;
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight - XNavigationHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorInset = UIEdgeInsetsMake(0, 11, 0, 11);
    _tableview.tableFooterView = [UIView new];
    [_tableview registerNib:[UINib nibWithNibName:@"ClassListTableViewCell" bundle:nil] forCellReuseIdentifier:@"ClassListTableViewCell"];
    [_tableview registerClass:[ClassListTableHeaderView class] forHeaderFooterViewReuseIdentifier:@"ClassListTableHeaderView"];
    [_tableview registerClass:[ClassListTableFooterView class] forHeaderFooterViewReuseIdentifier:@"ClassListTableFooterView"];
    _tableview.footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.view addSubview:_tableview];
    
    UIView *tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 90)];
    tableHeader.backgroundColor = [UIColor whiteColor];
    self.tableHeader = tableHeader;
    
    [tableHeader addSubview:_typeCollection];
    [tableHeader addSubview:_subtypeCollection];
    
    CALayer *line = [[CALayer alloc] init];
    line.frame = CGRectMake(0, 39.5, ScreenWidth, 0.5);
    line.backgroundColor = [UIColor colorWithRed:216/255.0 green:216/255.0 blue:216/255.0 alpha:1.0].CGColor;
    [tableHeader.layer addSublayer:line];
    
    CALayer *line2 = [[CALayer alloc] init];
    line2.frame = CGRectMake(0, 89.5, ScreenWidth, 0.5);
    line2.backgroundColor = [UIColor colorWithRed:216/255.0 green:216/255.0 blue:216/255.0 alpha:1.0].CGColor;
    [tableHeader.layer addSublayer:line2];
    _tableview.tableHeaderView = tableHeader;
    
    self.lineLayer = [[CALayer alloc] init];
    _lineLayer.frame = CGRectMake(0, 30, 15, 4);
    _lineLayer.cornerRadius = 2.0;
    _lineLayer.masksToBounds = YES;
    _lineLayer.backgroundColor = [UIColor colorWithRed:1.0 green:36/255.0 blue:69/255.0 alpha:1.0].CGColor;
    _lineLayer.hidden = YES;
    [_typeCollection.layer addSublayer:_lineLayer];
}

- (void)searchButtonPressed {
    
    UINavigationController *navi = [SearchListViewController searchController:searchClassClass];
    
    [self.navigationController presentViewController:navi animated:YES completion:nil];
}

- (void)loadData {
    
    self.dataArr = [NSMutableArray array];
    
    self.selectArray = @[@[], @[@"智能排序", @"最新上架", @"最近开课"], @[@"全部时间", @"今天", @"明天", @"本周末", @"一周内", @"一个月内"]];
    self.selectValueArray = @[@[], @[@"auto", @"last", @"recent"], @[@"", @"today", @"tomorrow", @"weekend", @"week", @"month"]];
    
    __weak typeof(self) weakself = self;
    if ([ClassTypeObject shared].allTypes.count == 0) {
        
        [[ClassTypeObject shared] requestClassTypes:^(NSArray<ClassTypeArrayModel *> *types) {
            weakself.typeArr = types;
            [weakself.typeCollection reloadData];
            [weakself.subtypeCollection reloadData];
            [weakself changeLineLayerPosition];
            weakself.lineLayer.hidden = NO;
            if (weakself.typeSelect == 0 || weakself.typeArr[weakself.typeSelect-1].children.count == 0) {
                weakself.tableHeader.frame = CGRectMake(0, 0, ScreenWidth, 40);
            }else {
                weakself.tableHeader.frame = CGRectMake(0, 0, ScreenWidth, 90);
            }
        }];
        
//        [HttpRequest getURL:kget_class_category paras:nil showProgress:NO successful:^(id  _Nullable result) {
//
//            NSMutableArray *array = [NSMutableArray array];
//            for (NSDictionary *dic in result[@"data"]) {
//                ClassTypeArrayModel *model = [[ClassTypeArrayModel alloc] initWithDic:dic];
//                [array addObject:model];
//            }
//            weakself.typeArr = array;
//            [weakself.typeCollection reloadData];
//            [weakself.subtypeCollection reloadData];
//            [weakself changeLineLayerPosition];
//            weakself.lineLayer.hidden = NO;
//        } failed:nil];
    }else {
        self.typeArr = [ClassTypeObject shared].allTypes;
        [self.typeCollection reloadData];
        [self.subtypeCollection reloadData];
        [self changeLineLayerPosition];
        self.lineLayer.hidden = NO;
        if (_typeSelect == 0 || self.typeArr[_typeSelect-1].children.count == 0) {
            self.tableHeader.frame = CGRectMake(0, 0, ScreenWidth, 40);
        }else {
            self.tableHeader.frame = CGRectMake(0, 0, ScreenWidth, 90);
        }
    }
    
    [HttpRequest getURL:kget_class_rec paras:nil showProgress:NO successful:^(id  _Nullable result) {
        
        NSArray *array = result[@"data"][@"items"];
        NSMutableArray *models = [NSMutableArray arrayWithCapacity:array.count];
        for (NSDictionary *dic in array) {
            ClassModel *model = [[ClassModel alloc] initWithDic:dic];
            [models addObject:model];
        }
        weakself.recommendsArr = models;
        [weakself.tableview reloadData];
        
    } failed:nil];
    
    NSMutableDictionary *paras = [NSMutableDictionary dictionary];
    if (_typeSelect != 0) {
        [paras setObject:[NSNumber numberWithInt:self.typeArr[_typeSelect-1]._id] forKey:@"id"];
    }
    
    [paras setObject:_selectValueArray[1][_sortSelect] forKey:@"sort"];
    [paras setObject:_selectValueArray[2][_timeSelect] forKey:@"time"];
    if (_areaCode) {
        [paras setObject:[LocationModel shared].areaNumber forKey:@"area"];
    }
    
    NSLog(@"%@", paras);
    self.paras = paras;
    [HttpRequest getURL:kget_class_search paras:paras showProgress:NO successful:^(id  _Nullable result) {
        
        NSArray *array = result[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            ClassModel *model = [[ClassModel alloc] initWithDic:dic];
            [weakself.dataArr addObject:model];
        }
        [weakself.tableview reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.tableview.footer endRefreshingWithNoMoreData];
        }else {
            weakself.model = model;
        }
    } failed:nil];
}

- (void)resetTableview {
    if (_tableview.footer.isRefreshing) {
        [_tableview.footer endRefreshing];
    }
    [self.tableview.footer resetNoMoreData];
}

- (void)reloadData {
    
    NSMutableDictionary *paras = [NSMutableDictionary dictionary];
    if (_typeSelect != 0) {
        if (_subtypeSelect != -1) {
            [paras setObject:[NSNumber numberWithInt:_typeArr[_typeSelect-1].children[_subtypeSelect]._id] forKey:@"id"];
        }else {
            [paras setObject:[NSNumber numberWithInt:_typeArr[_typeSelect-1]._id] forKey:@"id"];
        }
    }
    
    [paras setObject:_selectValueArray[1][_sortSelect] forKey:@"sort"];
    [paras setObject:_selectValueArray[2][_timeSelect] forKey:@"time"];
    if (_areaCode) {
        [paras setObject:[LocationModel shared].areaNumber forKey:@"area"];
    }
    
    NSLog(@"%@", paras);
    self.paras = paras;
    
    [self.dataArr removeAllObjects];
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kget_class_search paras:paras showProgress:NO successful:^(id  _Nullable result) {
        
        NSArray *array = result[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            ClassModel *model = [[ClassModel alloc] initWithDic:dic];
            [weakself.dataArr addObject:model];
        }
        [weakself.tableview reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.tableview.footer endRefreshingWithNoMoreData];
        }else {
            weakself.model = model;
        }
    } failed:nil];
}

- (void)loadMoreData {
    
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kget_class_search paras:_paras showProgress:NO successful:^(id  _Nullable result) {
        
        NSArray *array = result[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            ClassModel *model = [[ClassModel alloc] initWithDic:dic];
            [weakself.dataArr addObject:model];
        }
        [weakself.tableview reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.tableview.footer endRefreshingWithNoMoreData];
        }else {
            weakself.model = model;
            [weakself.tableview.footer endRefreshing];
        }
    } failed:^(int code, NSString *errMsg) {
        [weakself.tableview.footer endRefreshing];
    }];
}

- (UIView *)coverView {
    
    if (!_coverView) {
        
        _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - XNavigationHeight - 35)];
        _coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        _coverView.hidden = YES;
        [_coverView addSubview:self.selectTableView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideCoverView)];
        tap.delegate = self;
        [_coverView addGestureRecognizer:tap];
        
        [self.view addSubview:_coverView];
    }
    return _coverView;
}

- (void)hideCoverView {
    [_selectButton setImage:[UIImage imageNamed:@"gray_triangle"] forState:UIControlStateNormal];
    self.coverView.hidden = YES;
}

- (UITableView *)selectTableView {
    
    if (!_selectTableView) {
        _selectTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 200) style:UITableViewStylePlain];
        _selectTableView.delegate = self;
        _selectTableView.dataSource = self;
        _selectTableView.tag = 1;
        _selectTableView.backgroundColor = [UIColor whiteColor];
        _selectTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _selectTableView.tableFooterView = [UIView new];
    }
    return _selectTableView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (tableView.tag == 1) {
        return 0.01;
    }
    if (section == 0) {
        return 35.0;
    }
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (tableView.tag == 1) {
        return nil;
    }
    
    if (section == 0) {
        ClassListTableHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ClassListTableHeaderView"];
        view.delegate = self;
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (tableView.tag == 1) {
        return 0.01;
    }
    
    if (section == 0 && _dataArr.count == 0) {
        return 0.868 * ScreenWidth;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (tableView.tag == 1) {
        return nil;
    }
    
    if (section == 0 && _dataArr.count == 0) {
        ClassListTableFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ClassListTableFooterView"];
        view.delegate = self;
        return view;
    }
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView.tag == 1) {
        return 1;
    }
    if (_dataArr.count == 0) {
        return 2;
    }
    return 1;
}

- (void)footerViewSendButtonPressed {
    if ([UserModel shared].hasLogin == YES) {
        SendRequestViewController *con = [[SendRequestViewController alloc] init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:con];
        [self presentViewController:navi animated:YES completion:nil];
    }else {
        UINavigationController *controller = [LoginViewController loginViewController];
        [self presentViewController:controller animated:YES completion:nil];
    }
}

- (void)tableHeaderViewButton:(UIButton *)sender {
    
    CGRect headerFrame = [_tableview rectForHeaderInSection:0];
    headerFrame = [_tableview convertRect:headerFrame toView:self.view];
    if (headerFrame.origin.y < XNavigationHeight) {
        headerFrame = CGRectMake(0, XNavigationHeight, headerFrame.size.width, headerFrame.size.height);
    }
    NSLog(@"%@", NSStringFromCGRect(headerFrame));
    self.selectIndex = sender.tag;
    
    if (_coverView && _coverView.hidden == NO) {
        if (_selectButton && _selectButton.tag != sender.tag) {
            [_selectButton setImage:[UIImage imageNamed:@"gray_triangle"] forState:UIControlStateNormal];
            if (sender.tag == 0) {
                self.coverView.hidden = YES;
                [self toSelectArea];
            }else {
                [sender setImage:[UIImage imageNamed:@"red_triangle"] forState:UIControlStateNormal];
                [self.selectTableView reloadData];
            }
        }else {
            _coverView.hidden = YES;
            [sender setImage:[UIImage imageNamed:@"gray_triangle"] forState:UIControlStateNormal];
        }
    }else {
        if (sender.tag == 0) {
            [self toSelectArea];
        }else {
            self.coverView.frame = CGRectMake(0, CGRectGetMaxY(headerFrame), ScreenWidth, self.coverView.frame.size.height);
            self.coverView.hidden = NO;
            [sender setImage:[UIImage imageNamed:@"red_triangle"] forState:UIControlStateNormal];
            [self.selectTableView reloadData];
        }
    }
    self.selectButton = sender;
}

- (void)toSelectArea {
    
    JFCityViewController *controller = [[JFCityViewController alloc] init];
    self.selectingArea = YES;
    [self.navigationController presentViewController:controller animated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView.tag == 1) {
        return _selectArray[_selectIndex].count;
    }
    if (section == 0) {
        return _dataArr.count;
    }else if (_dataArr.count == 0) {
        return _recommendsArr.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 1) {
        return 45.0;
    }
    
    const CGFloat height = (ScreenWidth - 20.0) * 0.39 * 0.586 + 32;
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 1) {
        
        static NSString *identity = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identity];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    
    static NSString *identity = @"ClassListTableViewCell";
    ClassListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity forIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 1) {
        
        if (_selectIndex == 0) {
            
        }else if (_selectIndex == 1) {
            
            if (indexPath.row == _sortSelect) {
                cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:_selectArray[_selectIndex][indexPath.row] attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:1.0 green:36/255.0 blue:69/255.0 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:13.0]}];
            }else {
                cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:_selectArray[_selectIndex][indexPath.row] attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:45/255.0 green:45/255.0 blue:45/255.0 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:13.0]}];
            }
        }else {
            if (indexPath.row == _timeSelect) {
                cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:_selectArray[_selectIndex][indexPath.row] attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:1.0 green:36/255.0 blue:69/255.0 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:13.0]}];
            }else {
                cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:_selectArray[_selectIndex][indexPath.row] attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:45/255.0 green:45/255.0 blue:45/255.0 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:13.0]}];
            }
        }
    }else {
        
        ClassModel *model = nil;
        if (_dataArr.count == 0) {
            model = _recommendsArr[indexPath.row];
        }else {
            model = _dataArr[indexPath.row];
        }
        
        ClassListTableViewCell *ccell = (ClassListTableViewCell*)cell;
        [ccell.classImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb]];
        ccell.titleLabel.text = model.name;
        ccell.teacherLabel.text = model.teacher;
        ccell.timeLabel.text = model.begintime;
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"￥ " attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:1.0 green:0 blue:54/255.0 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:10.0]}];
        NSAttributedString *price = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f", model.price] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0]}];
        [string appendAttributedString:price];
        ccell.priceLabel.attributedText = string;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 1) {
        
        [self hideCoverView];
        
        if (_selectIndex == 1) {
            if (_sortSelect == indexPath.row) {
                return;
            }
            _sortSelect = indexPath.row;
            [self resetTableview];
            [self reloadData];
        }else if(_selectIndex == 2) {
            if (_timeSelect == indexPath.row) {
                return;
            }
            _timeSelect = indexPath.row;
            [self resetTableview];
            [self reloadData];
        }
        
        if (indexPath.row == 0) {
            [self.selectButton setAttributedTitle:nil forState:UIControlStateNormal];
            [self.selectButton setTitle:_selectArray[_selectIndex][indexPath.row] forState:UIControlStateNormal];
        }else {
            [self.selectButton setAttributedTitle:[[NSAttributedString alloc] initWithString:_selectArray[_selectIndex][indexPath.row] attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:1.0 green:36/255.0 blue:69/255.0 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:13.0]}] forState:UIControlStateNormal];
        }
    }else {
        
        NormalWebViewController *controller = [[NormalWebViewController alloc] init];
        if (_dataArr.count == 0) {
            controller.url = _recommendsArr[indexPath.row].link;
        }else {
            controller.url = _dataArr[indexPath.row].link;
        }
        
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([NSStringFromClass(touch.view.class) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
        
    }
    return YES;
}

#pragma mark - uicollectionview delegate

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    if (collectionView.tag == 0) {
        return UIEdgeInsetsMake(0, 10, 0, 10);
    }
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (collectionView.tag == 0) {
        if (_typeArr == nil) {
            return 0;
        }
        return _typeArr.count + 1;
    }else {
        if (_typeSelect == 0) {
            return 0;
        }
        return _typeArr[_typeSelect-1].children.count;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView.tag == 0) {
        
        if (indexPath.item == 0) {
            return CGSizeMake(50, 40);
        }
        
        CGRect rect = [_typeArr[indexPath.item-1].name boundingRectWithSize:CGSizeMake(100, 17) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13.0]} context:nil];
        return CGSizeMake(rect.size.width + 20, 40);
    }
    
    CGRect rect = [_typeArr[_typeSelect-1].children[indexPath.item].name boundingRectWithSize:CGSizeMake(100, 17) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13.0]} context:nil];
    return CGSizeMake(rect.size.width + 20, 50);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView.tag == 0) {
        
        ClassListTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassListTypeCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.titleLabel.text = @"全部";
        }else {
            cell.titleLabel.text = _typeArr[indexPath.item-1].name;
        }
        if (_typeSelect == indexPath.item) {
            cell.titleLabel.textColor = COLOR(255, 36, 69, 1);
        }else {
            cell.titleLabel.textColor = COLOR(128, 128, 128, 1);
        }
        return cell;
    }
    
    ClassListSubtypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassListSubtypeCollectionViewCell" forIndexPath:indexPath];
    cell.titleLabel.text = _typeArr[_typeSelect-1].children[indexPath.item].name;
    if (indexPath.item == _subtypeSelect) {
        cell.titleLabel.backgroundColor = COLOR(237, 237, 237, 1);
    }else {
        cell.titleLabel.backgroundColor = COLOR(247, 247, 247, 1);
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView.tag == 0) {
        
        if (indexPath.item == _typeSelect) {
            return;
        }
        _subtypeSelect = -1;
        _typeSelect = indexPath.item;
        [_typeCollection reloadData];
        [_subtypeCollection reloadData];
        [self changeLineLayerPosition];
        [self resetTableview];
        [self reloadData];
        if (_typeSelect == 0 || _typeArr[_typeSelect-1].children.count == 0) {
            self.tableHeader.frame = CGRectMake(0, 0, ScreenWidth, 40);
        }else {
            self.tableHeader.frame = CGRectMake(0, 0, ScreenWidth, 90);
        }
    }else {
        if (indexPath.item == _subtypeSelect) {
            return;
        }
        _subtypeSelect = indexPath.item;
        [_subtypeCollection reloadData];
        [self resetTableview];
        [self reloadData];
    }
}

- (void)changeLineLayerPosition {
    
    [self.typeCollection layoutIfNeeded];
    UICollectionViewCell *cell = [self.typeCollection cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_typeSelect inSection:0]];
    [self.typeCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_typeSelect inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    CGRect frame = cell.frame;
    if (!cell) {
        UICollectionViewLayoutAttributes *att = [self.typeCollection.collectionViewLayout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:_typeSelect inSection:0]];
        frame = att.frame;
    }
    _lineLayer.position = CGPointMake(frame.origin.x + frame.size.width * 0.5, 32);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
